//
//  HOTEvaluationViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-26.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTEvaluationViewController.h"
#import "ZZZUIScrollView+Extends.h"

@interface HOTEvaluationViewController ()<UITextViewDelegate,ZZZPopupViewDelegate> {
    
    NSString *_uid;
}

@end

@implementation HOTEvaluationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil uid:(NSString *)uid
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        _uid = uid;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"評価する"];
    
    _titleBgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"value_title"]];
    
    _ratingView.paddingW = 10;
    [_ratingView setImagesDeselected:@"icon_star_l_off" partlySelected:@"icon_star_l2" fullSelected:@"icon_star_l_on" andDelegate:nil];
    
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_profile"]];
    
    _contentBgView.image = [_contentBgView.image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20)];
    
    _titleTextView.inputAccessoryView = [HOTUtils createInputAccessoryViewWithName:MSG_INPUT_BAR_NEXT target:self action:@selector(nextBtnClicked:)];
    
    _contentTextView.inputAccessoryView = [HOTUtils createInputAccessoryViewWithName:MSG_INPUT_BAR_DONE target:self action:@selector(doneBtnClicked:)];
    
    [self addRightBtnTarget:self action:@selector(sendBtnClicked) image:[UIImage imageNamed:@"btn_send_off"] hImage:[UIImage imageNamed:@"btn_send_on"]];
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {

    [_mainScrollView scrollViewToVisible:textView.superview offsetY:120 animated:YES];

    return YES;
}

-(void)sendBtnClicked {
    
    
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [_contentTextView resignFirstResponder];
    [_titleTextView resignFirstResponder];
    
    ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                          message:MSG_IS_SEND
                                                         delegate:self
                                                cancelButtonTitle:ALERT_BTN_CANCEL
                                                 otherButtonTitle:ALERT_BTN_OK];
    popupView.tag = 101;
    [popupView showInWindow];
}

-(void)requestUpdate {
    
    if (![self checkParametersValid]) {
        return;
    }
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_TARGET_UID:_uid,
                                 REQ_KEY_SCORE:@(_ratingView.rating),
                                 REQ_KEY_RATE_TITLE:_titleTextView.text,
                                 REQ_KEY_RATE_CONTENT:_contentTextView.text};
    
    __unsafe_unretained HOTEvaluationViewController *tempObj = self;
    [self requestWithURL:HOT_URL_USER_APPRAISAL Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        tempObj.popVcExcute = YES;
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^{
            
            [tempObj.navigationController popViewControllerAnimated:YES];
        }];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

-(BOOL)checkParametersValid {
    
    NSString *errName = @"";
    HOTInputItemType inputType = 0;
    if (!_contentTextView.text || [_contentTextView.text isEqualToString:@""]) {
        
        errName = @"評価内容";
        inputType = HOTInputItemText;
    } 
    
    if (errName.length > 0) {
        
        NSString * errMsg = [self getErrMsgWith:errName itemType:inputType];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:errMsg delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];

        [popupView showInWindow];
        
        return NO;
    }
    
    return YES;
}

-(void)popupView:(ZZZPopupView *)popupView buttonKind:(ZZZPopupViewBtnKind)btnKind {
    [super popupView:popupView buttonKind:btnKind];
    if (popupView.tag == 101 && btnKind == ZZZPopupViewConfirmBtnKind) {
        
        [self requestUpdate];
    }
}

-(void)doneBtnClicked:(id)sender {
    
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [_contentTextView resignFirstResponder];
}

-(void)nextBtnClicked:(id)sender {

    //[_mainScrollView setContentOffset:CGPointMake(0, 100) animated:YES];
    [_contentTextView becomeFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
